package com.eastrobot.doc.poitlconvert.service.impl;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.deepoove.poi.render.compute.EnvModel;
import com.deepoove.poi.render.compute.ReadMapAccessor;
import com.deepoove.poi.render.compute.RenderDataCompute;
import com.eastrobot.doc.poitlconvert.entity.mytemplateEntity.MyTemplateData;
import com.eastrobot.doc.poitlconvert.util.MvelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

@Slf4j
public class CustomerDataCompute implements RenderDataCompute {
    private boolean isStrict;
//    private CustomerDataEvaluationContext context;
    private   EvaluationContext context;
    private EvaluationContext envContext;
    public CustomerDataCompute(EnvModel model) {
        this(model, true);
    }

    public CustomerDataCompute(EnvModel model, boolean isStrict) {
        this(model, isStrict, Collections.emptyMap());
    }

    public CustomerDataCompute(EnvModel model, boolean isStrict, Map<String, Method> spELFunction) {
        this.isStrict = isStrict;
        if (null != model.getEnv() && !model.getEnv().isEmpty()) {
            this.envContext = new CustomerDataEvaluationContext(model.getEnv());
            ((CustomerDataEvaluationContext)this.envContext).addPropertyAccessor(new ReadMapAccessor());
        }
        this.context = new CustomerDataEvaluationContext(model.getRoot());
        ((CustomerDataEvaluationContext)this.context).addPropertyAccessor(new ReadMapAccessor());
//        CustomerDataEvaluationContext var10001 = (CustomerDataEvaluationContext)this.context;
//        spELFunction.forEach(var10001::registerFunction);
    }
    public Object compute(String el) {
        try {
            Object value = ((CustomerDataEvaluationContext)this.context).getValue();
            Map value1  =new LinkedHashMap();
            if(ObjectUtil.isNotEmpty(value)){
                if(value instanceof Map  ){
                    value1 = (Map) value;

                } if(value instanceof List){
                    value1 =  ((List<?>) value).stream().collect(Collectors.toMap(item-> {
                        return  ((List<?>) value).indexOf(item);
                    }, Function.identity()));
                } else{
                    value1 = BeanUtil.beanToMap(value);
                }
            }

            Object eval = MvelUtil.eval(el,value1 );
            System.out.println(eval);
            return eval;
        } catch (Exception var4) {
            if (this.isStrict) {
                log.error(var4.getMessage(),var4);
                // 抛出错误
                throw var4;
            } else {
                return "没有数据";
            }
        }
    }
}
